package com.khshop.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.khshop.pojo.Address;
import com.khshop.mapper.AddressMapper;
import com.khshop.pojo.Comment;
import com.khshop.service.AddressService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.khshop.vo.ServiceResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 地址 服务实现类
 * </p>
 *
 * @author zhaoyang
 * @since 2020-10-15
 */
@Service
public class AddressServiceImpl extends ServiceImpl<AddressMapper, Address> implements AddressService {

    @Autowired(required = false)
    AddressMapper addressMapper;

    //新增地址
    public ServiceResult<Address> addAddress(Address address){
        return ServiceResult.of(addressMapper.insert(address)>0);

    }

    //修改地址
    public ServiceResult<Address> updateAddress(Address address){
        return ServiceResult.of(addressMapper.updateById(address)>0);

    }

    //删除地址
    public ServiceResult<Address> delAddress(Long id){
        return ServiceResult.of(addressMapper.deleteById(id)>0);

    }

    //设置默认地址
    public ServiceResult<Address> setDefaultAddress(Long id){
        Address address = addressMapper.selectById(id);
        address.setTag("默认地址");
        return ServiceResult.of(addressMapper.updateById(address)>0);
    }

    //查询地址
    public ServiceResult<IPage<Address>> searchAddress(Long userid, Integer page, Integer limit){
        QueryWrapper<Address> commentQueryWrapper = new QueryWrapper<>();
            commentQueryWrapper=commentQueryWrapper.eq("userid",userid);
        Page<Address> iPage = new Page<Address>(page, limit);
        IPage<Address> orderIPage = addressMapper.selectPage(iPage, commentQueryWrapper);
        return ServiceResult.of(orderIPage,orderIPage.getSize()>0);

    }
    //查询地址
    public ServiceResult<Address> getAddress(Long id){
        QueryWrapper<Address> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",id);
        Address address = addressMapper.selectOne(queryWrapper);
        return ServiceResult.of(address,address==null);
    }
}
